﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class XSSJ_XSSJAdd : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("6"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      if (CWSD.GetSDZT("是"))
      {
        Server.Transfer("~\\sd.aspx");
      }
      DataSet ds = CBR.GetDS_W();
      CommonUse.BindDDL(ds, "cbrid", "cbr", "0", ddlCBR);
      string sID = CommonUse.GetID("id");
      if (!string.IsNullOrEmpty(sID))
      {
        InitData(sID);
        ASPxHyperLink1.NavigateUrl = "XSSJList.aspx";
        hlQLMX.NavigateUrl = "XSSJQLMX.aspx?mode=0&id=" + CommonUse.GetID();
      }
      else
      {
        dateJG.Text = DateTime.Now.ToString("yyyy-MM-dd");
        ASPxHyperLink1.NavigateUrl = "XSSJKHList.aspx";
        hlQLMX.NavigateUrl = "XSSJQLMX.aspx?mode=1&id=" + CommonUse.GetID();
      }
      InitKHXX(sID);
      InitSC(sID);
    }
  }

  protected void InitKHXX(string sID)
  {
    if (string.IsNullOrEmpty(sID))
    {
      lbKHID.Text = CommonUse.GetID("khid");
    }
    else
    {
      lbKHID.Text = CBSJ.GetSKHID(sID);
    }
    SqlDataReader dr = KHXX.GetXX(lbKHID.Text);
    if (dr.Read())
    {
      lbKHMC.Text = dr["khmc"].ToString();
      lbBS.Text = dr["kh"].ToString() + "&nbsp;";
    }
    dr.Close();
  }

  protected void InitSC(string sID)
  {
    SqlDataReader dr = CBSJ.GetSCCB(lbKHID.Text, string.IsNullOrEmpty(sID) ? DateTime.Now.ToString("yyyyMMdd") : dateJG.Text);
    if (dr.Read())
    {
      lbSCBS.Text = dr["bybs"].ToString() + "&nbsp;";
      lbSCCBR.Text = dr["cbr"].ToString() + "&nbsp;";
      lbSCCBSJ.Text = dr["rq"].ToString() + "&nbsp;";
    }
    dr.Close();
  }

  protected void InitData(string sID)
  {
    SqlDataReader dr = CBSJ.GetCBSJ(sID);
    if (dr.Read())
    {
      txtName.Text = dr["bybs"].ToString();
      hlName.Value = txtName.Text;
      dateJG.Text = dr["rq"].ToString();
      hlJG.Value = dateJG.Text;
      //sKHID = dr["khid"].ToString();
      ddlCBR.SelectedValue = dr["cbrid"].ToString();
      hlCBR.Value = ddlCBR.SelectedValue;
      //ddlYQXZ.SelectedValue = dr["yqxz"].ToString();
      //dateJG.Enabled = false;
    }
    dr.Close();
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    //if (string.IsNullOrEmpty(dateJG.Text))
    //{
    //  lbMessage.Text = "抄表时间不能为空";
    //  return;
    //}
    if (string.IsNullOrEmpty(CommonUse.GetID()))
      Insert();
    else
      Update();
  }

  protected void Save(string sID)
  {
    if (ddlCBR.SelectedValue == "0")
    {
      lbMessage.Text = "请选择抄表人";
      return;
    }
    if (CBSJ.ExistHQID(lbKHID.Text, dateJG.Text))
    {
      lbMessage.Text = "本次日期之后已存在数据，不能修改";
      return;
    }
    if (CBSJ.ExistWSBSJ(lbKHID.Text, dateJG.Text))
    {
      lbMessage.Text = "存在未上报数据，请先处理";
      return;
    }
    if (CBSJ.ExistWSHSJ(lbKHID.Text, dateJG.Text))
    {
      lbMessage.Text = "存在已上报未审核数据，请先处理";
      return;
    }
    //if (!CommonUse.IsInt(txtName))
    //{
    //  lbMessage.Text = "本次表数必须为整数";
    //  return;
    //}

    string sSYBS = CBSJ.GetSCBS(lbKHID.Text, Convert.ToDateTime(dateJG.Text));
    string sSCCBSJ = CBSJ.GetSCCBSJ(lbKHID.Text, Convert.ToDateTime(dateJG.Text));

    DataSet dsYQXZ = KHXX.GetYQXZ(lbKHID.Text);
    if (dsYQXZ.Tables[0].Rows.Count == 0)
    {
      lbMessage.Text = "该用户未选择用气性质，请与管理员联系";
      return;
    }

    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;
    ArrayList arrValue = new ArrayList();
    ArrayList arrLengh = new ArrayList();
    #region  抄表数据
    cis.Add(CommonUse.DeleteCI("SJLR_CBSJ", "cbsjid", sID));

    arrValue = new ArrayList();
    arrValue.Add(sID);
    arrValue.Add(SysManage.Get_User);
    arrValue.Add(ddlCBR.SelectedValue);
    arrValue.Add(lbKHID.Text);
    arrValue.Add(dateJG.Text);
    arrValue.Add(txtName.Text);
    arrValue.Add((Convert.ToDouble(txtName.Text) - Convert.ToDouble(sSYBS)).ToString());
    arrValue.Add(DBNull.Value);
    arrValue.Add("0");
    arrValue.Add(DBNull.Value);
    arrValue.Add(DBNull.Value);
    arrValue.Add(DBNull.Value);
    arrValue.Add((Convert.ToDouble(txtName.Text) - Convert.ToDouble(sSYBS)).ToString());
    arrValue.Add(DBNull.Value);
    arrValue.Add("0");
    arrValue.Add(sSYBS);
    arrValue.Add(string.IsNullOrEmpty(sSCCBSJ) ? Convert.DBNull : sSCCBSJ);
    arrLengh = new ArrayList();
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    arrLengh.Add("20");
    cis.Add(CommonUse.InsertCI("sjlr_cbsj", arrValue, arrLengh));
    #endregion
    #region  本期气量
    cis.Add(CommonUse.DeleteCI("SJLR_bqql", "cbsjid", sID));

    if (dsYQXZ.Tables[0].Rows.Count == 1)
    {
      DataTable dtJG = JGGL.GetJG(dsYQXZ.Tables[0].Rows[0]["yqxzid"].ToString(), dateJG.Text);
      if (dtJG.Rows.Count == 0)
      {
        lbMessage.Text = "还未录入价格，请与管理员联系";
        return;
      }

      arrValue = new ArrayList();
      arrValue.Add(dsYQXZ.Tables[0].Rows[0]["yqxzid"].ToString());
      arrValue.Add(sID);
      arrValue.Add((Convert.ToDouble(txtName.Text) - Convert.ToDouble(sSYBS)).ToString());
      arrValue.Add("0");
      arrValue.Add(dtJG.Rows[0]["jg"].ToString());
      arrValue.Add(dtJG.Rows[0]["jj"].ToString());
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrValue.Add(DBNull.Value);
      arrLengh = new ArrayList();
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      arrLengh.Add("20");
      cis.Add(CommonUse.InsertCI("sjlr_bqql", arrValue, arrLengh));

      sSql = @"update sjlr_bqql set ysjg=ceiling(jg*bqql*100)/100,ysjj=ceiling(jj*bqql*100)/100,
      ysfy=ceiling((jg+jj)*bqql*100)/100,ssql=(-1)*bqql,ssjg=(-1)*ceiling(jg*bqql*100)/100,ssjj=(-1)*ceiling(jj*bqql*100)/100,
      ssfy=(-1)*ceiling((jg+jj)*bqql*100)/100
      where yqxzid=@0 and cbsjid=@1";
      cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 12) };
      cmdParams[0].Value = dsYQXZ.Tables[0].Rows[0]["yqxzid"].ToString();
      cmdParams[1].Value = sID;
      ci = new CommandInfo();
      ci.CommandText = sSql;
      ci.Parameters = cmdParams;
      cis.Add(ci);
    }
    #endregion
    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "保存成功";
      //ProjectPrivate.CreateZHCX(sKHID, Convert.ToDateTime(dateJG.Text).ToString("yyyy-MM-dd"));
      //ProjectPrivate.UpdateCB(sKHID, Convert.ToDateTime(dateJG.Text).ToString("yyyy-MM-dd"));
      if (string.IsNullOrEmpty(CommonUse.GetID("id")))
      {
        hlQLMX.NavigateUrl = "XSSJQLMX.aspx?mode=1&id=" + sID;
        Server.Transfer("XSSJQLMX.aspx?mode=1&id=" + sID);
      }
      else
      {
        string nr = "";
        if (txtName.Text != hlName.Value)
          nr += "表数由" + hlName.Value + "改为" + txtName.Text + "  ";
        if (dateJG.Text != hlJG.Value)
          nr += "日期由" + hlJG.Value + "改为" + dateJG.Text + "  ";
        if (ddlCBR.SelectedValue != hlCBR.Value)
          nr += "抄表人由" + ddlCBR.Items.FindByValue(hlCBR.Value).Text + "改为" + ddlCBR.SelectedItem.Text + "  ";
        if (nr != "")
          rzcz.InsertRZ("修改抄表数据:客户编码为" + lbKHID.Text + ",日期为" + dateJG.Text + nr);
        hlQLMX.NavigateUrl = "XSSJQLMX.aspx?mode=0&id=" + sID;
        Server.Transfer("XSSJQLMX.aspx?mode=0&id=" + sID);
      }
    }

  }

  //插入
  protected void Insert()
  {
    string sID = CommonUse.GetID("sjlr_cbsj", "cbsjid");
    if (CBSJ.ExistID(lbKHID.Text, Convert.ToDateTime(dateJG.Text).ToString("yyyyMMdd")))
    {
      lbMessage.Text = "此客户于今日已录入";
      return;
    }
    Save(sID);
  }

  //修改
  protected void Update()
  {
    Save(CommonUse.GetID());
  }

}
